//
//  LoginViewController.m
//  Kjs-iOS
//
//  Created by jw on 16/6/3.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPswViewController.h"
#import "KJSRequest.h"
#import "AppDelegate.h"
#import "CommonUtil.h"
#import "MainTabBarController.h"

#define CurentDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
#define SCREEN_W [[UIScreen mainScreen] bounds].size.width //屏幕宽度
#define SCREEN_H [[UIScreen mainScreen] bounds].size.height //屏幕高度
#define NAV_BAR_H self.navigationController.navigationBar.frame.size.height

@interface LoginViewController ()
@property (nonatomic,assign) Boolean isVisiable;
@property (nonatomic,strong) NSString *pswStr;

@property (nonatomic,weak) IBOutlet UIButton *remenberPswBtn;
@property (nonatomic,strong) NSString *ifRemenberPsw;
@property (nonatomic,weak) IBOutlet UIButton *autoLoginBtn;
@property (nonatomic,strong) NSString *ifAutoLogin;

@property (nonatomic,weak) IBOutlet UILabel *telLable;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.loginBtn.layer.cornerRadius = 10;
    [self.loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    //设注册按钮
    self.registerBtn.layer.cornerRadius = 10;
    [self.registerBtn addTarget:self action:@selector(goRegister) forControlEvents:UIControlEventTouchUpInside];
    //注册按钮外边框设置
    self.registerBtn.layer.borderWidth = 1;
    [self.registerBtn.layer setBorderColor:[UIColor whiteColor].CGColor];
    
    //设置忘记密码按钮
    [self.forgetPswBtn addTarget:self action:@selector(goForgetPsw) forControlEvents:UIControlEventTouchUpInside];
    
    //关闭按钮
    [self.dismissBtn addTarget:self action:@selector(dismissLoginView) forControlEvents:UIControlEventTouchUpInside];

    //可见行切换
    [self.pswEyeBtn addTarget:self action:@selector(swichPasswordVisiable) forControlEvents:UIControlEventTouchUpInside];
    
    self.ifRemenberPsw = @"unremeber";
    self.ifAutoLogin = @"unautoLogin";
    [self.remenberPswBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];
    [self.autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];
    [self.remenberPswBtn addTarget:self action:@selector(ifRemenberPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.autoLoginBtn addTarget:self action:@selector(ifAutoLoginUser) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.password.secureTextEntry = YES;
    self.isVisiable = false;
    self.pswStr = @"";
    [self.navigationController setNavigationBarHidden:YES];
    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
    if ([userDefaults objectForKey:@"username"] != NULL) {
        self.username.text = [userDefaults objectForKey:@"username"];
        if ([(NSString *)([userDefaults objectForKey:@"ifRemenberPsw"]) isEqualToString:@"remeber"]) {
            self.password.text = [userDefaults objectForKey:@"password"];
            self.ifRemenberPsw = [userDefaults objectForKey:@"ifRemenberPsw"];
        }
    }
    
}
-(void)dismiss:(BOOL)animated
{
    [super dismiss:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
-(void)displayViewController:(UIViewController *)viewController
{
    [super displayViewController:viewController];
    [self.navigationController setNavigationBarHidden:NO];
}
#pragma mark - overwirte
-(void)setIfAutoLogin:(NSString *)ifAutoLogin
{
    _ifAutoLogin = ifAutoLogin;
    if ([_ifAutoLogin isEqualToString:@"autoLogin"]) {
        self.ifRemenberPsw = @"remeber";
        [self.autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-checked"] forState:UIControlStateNormal];
    }else if ([_ifAutoLogin isEqualToString:@"unautoLogin"]) {
        [self.autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];
    }
}
-(void)setIfRemenberPsw:(NSString *)ifRemenberPsw
{
    _ifRemenberPsw = ifRemenberPsw;
    if ([_ifRemenberPsw  isEqualToString:@"remeber"]) {
        [self.remenberPswBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-checked"] forState:UIControlStateNormal];
    }else if ([_ifRemenberPsw isEqualToString:@"unremeber"]) {
        [self.remenberPswBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];
    }

}
#pragma mark - custom method
-(void)ifRemenberPassword
{
    self.ifRemenberPsw = [self.ifRemenberPsw isEqualToString:@"remeber"]?@"unremeber":@"remeber";
}
-(void)ifAutoLoginUser
{
    self.ifAutoLogin = [self.ifAutoLogin isEqualToString:@"autoLogin"]?@"unautoLogin":@"autoLogin";
}

-(void)swichPasswordVisiable
{
    self.pswStr = self.password.text;
    self.password.text = @"";
    self.password.secureTextEntry = !self.password.secureTextEntry;
    self.password.text = self.pswStr;
}
-(void)dismissLoginView
{
    [self dismiss:YES];
}
//验证登录
- (void)login
{
    [self.view endEditing:YES];
    if ([self.username.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if ([self.password.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入密码" message:nil waitingTime:1.5];
    }else{
        [KJSRequest LoginWithUsername:self.username.text Password:self.password.text ret:^(UserInfo *model){
            //保存已登录的信息
            NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
            [userDefaults setObject:self.username.text forKey:@"username"];
            [userDefaults setObject:self.ifRemenberPsw forKey:@"ifRemenberPsw"];
            [userDefaults setObject:self.ifAutoLogin forKey:@"ifAutoLogin"];
            [userDefaults setObject:self.password.text forKey:@"password"];
            CurentDelegate.account = model;
//            CurentDelegate.IMpassword = @"zpf123";
//            CurentDelegate.IMuserID = @"zpf";
            //登录状态改变，首页客服页面重新加载
//            CurentDelegate.ifReLoad = YES;
            
            [self dismiss:YES];
            if(self.resultAction) {
                NSLog(@"运行回调");
                self.resultAction(model);
            }
        }];
    }
    
}
     
- (void) goForgetPsw
{
    NSLog(@"忘记密码");
    ForgetPswViewController *forgetPswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil]instantiateViewControllerWithIdentifier:@"ForgetPswViewController"];
    forgetPswVC.updatePswSuccess = ^(NSString *username,NSString *psw)
    {
        self.username.text = username;
        self.password.text = psw;
    };
    [self displayViewController:forgetPswVC];
}
- (void) goRegister
{
    NSLog(@"注册");
    RegisterViewController *registerVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"RegisterViewController"];
    registerVC.successRegister = ^(NSString *username,NSString *psw){
        self.username.text = username;
        self.password.text = psw;
    };
    [self displayViewController:registerVC];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
